package pl.javarsovia.javarsovia4android.adapters;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.domain.Partner;
import pl.javarsovia.javarsovia4android.domain.PartnerCategory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author mateusz.grzechocinski@gmail.com
 * @since 2010-06-19
 */
public class ExpandablePartnersListAdapter extends BaseExpandableListAdapter {

	private Context ctx;
	private Map<PartnerCategory, List<Partner>> partnersByCategories;
	private List<PartnerCategory> categories;

	public ExpandablePartnersListAdapter(Context androidContext, List<Partner> partnersOfType) {
		this.ctx = androidContext;
		partnersByCategories = Partner.groupByCategories(partnersOfType);

		categories = new ArrayList<PartnerCategory>(partnersByCategories.keySet());
		Collections.sort(categories);
	}

	public Object getChild(int groupPosition, int childPosition) {
		PartnerCategory category = categories.get(groupPosition);
		return partnersByCategories.get(category).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		PartnerCategory category = categories.get(groupPosition);
		return partnersByCategories.get(category).size();
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		final LayoutInflater layoutInflater = LayoutInflater.from(ctx);
		View view = layoutInflater.inflate(R.layout.partner_row, parent, false);
		Partner partner = (Partner) getChild(groupPosition, childPosition);
		ImageView logo = (ImageView) view.findViewById(R.id.partner_row_logo);
		logo.setImageResource(partner.getLogoResId());
		logo.setOnClickListener(createOnLogoClickListener(partner));
		return view;
	}

	public Object getGroup(int groupPosition) {
		return categories.get(groupPosition);
	}

	public int getGroupCount() {
		return categories.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		final LayoutInflater layoutInflater = LayoutInflater.from(ctx);
		View view = layoutInflater.inflate(R.layout.partner_category_row, parent, false);
		TextView partnerCategoryName = (TextView) view.findViewById(R.id.partner_category_row_name);
		PartnerCategory partnerCategory = (PartnerCategory) getGroup(groupPosition);
		partnerCategoryName.setText(partnerCategory.getLabelResId());
		return view;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	private View.OnClickListener createOnLogoClickListener(final Partner partner) {
		return new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				goToPartnerWebPage();
			}

			private void goToPartnerWebPage() {
				Intent openBrowserIntent = new Intent("android.intent.action.VIEW", Uri.parse(partner.getWebsiteUrl()));
				ctx.startActivity(openBrowserIntent);
			}
		};
	}
}
